package my.soft.aiteacher.teacher.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import my.soft.aiteacher.exception.BusinessException;
import my.soft.aiteacher.teacher.mapStruct.KeyPointItemMapstruct;
import my.soft.aiteacher.teacher.mapStruct.KeyPointMapstruct;
import my.soft.aiteacher.teacher.mapper.KeyPointItemMapper;
import my.soft.aiteacher.teacher.mapper.KeyPointMapper;
import my.soft.aiteacher.teacher.pojo.KeypointDO;
import my.soft.aiteacher.teacher.pojo.KeypointItemDO;
import my.soft.aiteacher.teacher.pojo.KeypointItemVO;
import my.soft.aiteacher.teacher.pojo.KeypointVO;
import my.soft.aiteacher.teacher.service.IKeyPointService;
import my.soft.aiteacher.utils.RedisIdUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class KeyPointServiceImpl extends ServiceImpl<KeyPointMapper, KeypointDO> implements IKeyPointService {
    @Resource
    private KeyPointMapper keyPointMapper;

    @Resource
    private KeyPointItemMapper keyPointItemMapper;

    @Resource
    private KeyPointMapstruct keyPointMapstruct;

    @Resource
    private KeyPointItemMapstruct keyPointItemMapstruct;

    @Resource
    private RedisIdUtils redisIdUtils;
    @Override
    @Transactional
    public void submitQuestionnaire(KeypointVO keypointVO) {
        KeypointDO keypointDO = keyPointMapper.selectOne(new QueryWrapper<KeypointDO>().eq("file_id", keypointVO.getFileId()));
        if(Objects.nonNull(keypointDO)){
           keyPointItemMapper.delete(new QueryWrapper<KeypointItemDO>().eq("keypoint_id",keypointDO.getId()));
           keyPointMapper.deleteById(keypointDO.getId());
        }
        Long questionnaireId = redisIdUtils.getId("keypoint:"+System.currentTimeMillis());
        keypointVO.setId(questionnaireId);
        keyPointMapper.insert(keyPointMapstruct.toQuestionnaireDO(keypointVO));
        List<KeypointItemVO> items = keypointVO.getItems();
        List<KeypointItemDO> keypointItemDOList = new ArrayList<>();
        for (KeypointItemVO item : items) {
            item.setKeypointId(questionnaireId);
            KeypointItemDO keypointItemDO = keyPointItemMapstruct.toQuestionnaireItemDO(item);
            keypointItemDOList.add(keypointItemDO);
        }
        keyPointItemMapper.insert(keypointItemDOList);
    }

    @Override
    public KeypointVO queryQuestionnaire(Long fileId) {
        KeypointDO keypointDO = keyPointMapper.selectOne(new QueryWrapper<KeypointDO>().eq("file_id",fileId));
        if(Objects.isNull(keypointDO)){
            throw new BusinessException("视频节点不存在");
        }
        List<KeypointItemDO> keypointItemDOList = keyPointItemMapper.selectList(new QueryWrapper<KeypointItemDO>().eq("keypoint_id", keypointDO.getId()));
        if(Objects.isNull(keypointItemDOList)){
            throw new BusinessException("视频节点题目为空");
        }
        log.info("keypointItemDOList:{}",keypointItemDOList);
        List<KeypointItemVO> keypointItemVOList = new ArrayList<>();
        keypointItemDOList.forEach(keypointItemDO -> {
            KeypointItemVO keypointItemVO = keyPointItemMapstruct.toQuestionnaireItemVO(keypointItemDO);
            keypointItemVOList.add(keypointItemVO);
        });
        KeypointVO keypointVO = keyPointMapstruct.toQuestionnaireVO(keypointDO);
        keypointVO.setItems(keypointItemVOList);
        return keypointVO;
    }

    @Override
    @Transactional
    public void deleteKeypoint(Long id) {
        KeypointItemDO keypointItemDO = keyPointItemMapper.selectById(id);
        if(Objects.isNull(keypointItemDO)){
            throw new BusinessException("视频节点题目不存在");
        }
        keyPointItemMapper.deleteById(id);
    }

    @Override
    public void updatekeypoint(KeypointItemVO keypointItemVO) {
        KeypointItemDO keypointItemDO = keyPointItemMapper.selectOne(new QueryWrapper<KeypointItemDO>().eq("id", keypointItemVO.getId()));
        if(Objects.isNull(keypointItemDO)){
            throw new BusinessException("视频节点题目不存在");
        }
        keyPointItemMapper.updateById(keyPointItemMapstruct.toQuestionnaireItemDO(keypointItemVO));
    }
}